package com.moyun.finance.service;

import com.moyun.finance.entity.*;
import com.moyun.finance.repository.*;
import com.moyun.finance.utils.BaseUtil;
import com.moyun.finance.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReceivablesService {
    @Autowired
    ContractRepository cr;
    @Autowired
    ReceivablesRepository rr;
    @Autowired
    ProductContractMidRepository pcmr;
    @Autowired
    OrgUserRepository our;
    @Autowired
    ProjectsRepository pr;
    @Autowired
    CustomerRepository cusr;
    @Autowired
    AccountRepository ar;
    @Autowired
    BillRepository br;
    @Autowired
    BillService bs;
    @Autowired
    ProductRepository pror;

    public Response save(Receivables r){
        r.setId(BaseUtil.uuid());
        r.setAlreadyMoney(0);
        r.setSurplusMoney(r.getReceivablesMoney());
        rr.save(r);
        return new Response("新增成功",true);
    }

    public Response delete(Receivables r){
        rr.delete(r);
        return new Response("删除成功",true);
    }

    public Response update(Receivables r){
        Receivables oldR = rr.findById(r.getId()).orElse(null);
        BigDecimal oldAlreadyMoney = new BigDecimal(oldR.getAlreadyMoney());//旧已收(付)
        BigDecimal newAlreadyMoney = new BigDecimal(r.getAlreadyMoney());//这次收(付)的
        BigDecimal alreadyMoney = oldAlreadyMoney.add(newAlreadyMoney);//已收(付)
        BigDecimal receivablesMoney = new BigDecimal(r.getReceivablesMoney());//应收(付)
        BigDecimal surplusMoney = receivablesMoney.subtract(alreadyMoney);//剩余应收(付)
        Bill bill = new Bill();
//        bill.setId(BaseUtil.uuid());
        bill.setContractId(r.getContractId());
        bill.setCompanyId(r.getCompanyId());
        if(r.getContractId() != null){
            Contract con = cr.findById(r.getContractId()).orElse(null);//此条应收(付)关联的合同
            bill.setUserId(con.getLeadingId());
            bill.setCustomerId(con.getCustomerId());
            bill.setProjectsId(con.getProjectsId());
        }
        bill.setBillMoney(r.getAlreadyMoney());
        bill.setAccountId(r.getAccountId());
        //账单类型 收入or支出
        if(r.getState() == 0){//应付=支出
            bill.setBillType(0);
        }else{
            bill.setBillType(1);
        }
        bs.save(bill);
        r.setAlreadyMoney(alreadyMoney.doubleValue());
        r.setSurplusMoney(surplusMoney.doubleValue());
        rr.save(r);
        return new Response("修改成功",r,true);
    }

    //该公司应收款列表
    public Response findReceivablesList(String companyId) throws ParseException{
        List<Receivables> rs = rr.findByCompanyIdAndStateAndSurplusMoneyGreaterThan(companyId, 1, 0);
        List<Receivables> returnList = new ArrayList<>();
        for(Receivables r : rs){
            String date = r.getReceivablesDate();//应收款时间
            if(DateUtils.isValidDate(date) && r.getReceivablesMoney() > 0){
                returnList.add(r);
            }
        }
        returnUtil(returnList);
        return new Response("",returnList,true);
    }

    //该公司应付款列表
    public Response findPaymentList(String companyId) throws ParseException{
        List<Receivables> rs = rr.findByCompanyIdAndStateAndSurplusMoneyGreaterThan(companyId, 0, 0);
        List<Receivables> returnList = new ArrayList<>();
        for(Receivables r : rs){
            String date = r.getReceivablesDate();//应付款时间
            if(DateUtils.isValidDate(date) && r.getReceivablesMoney() > 0){
                returnList.add(r);
            }
        }
        returnUtil(returnList);
        return new Response("",returnList,true);
    }

    public Response findOne(String receivablesId){
        Receivables r = rr.findById(receivablesId).orElse(null);
        returnUtil(r);
        return new Response("",r,true);
    }

    //返回值处理
    public List<Receivables> returnUtil(List<Receivables> rs){
        for(Receivables r : rs){
//            if(r.getContractId() == null){
//                continue;
//            }
//            Contract c = cr.findById(r.getContractId()).orElse(null);
//            r.setContract(c);
            if(r.getContractId() != null && !"".equals(r.getContractId())){
                Contract c = cr.findById(r.getContractId()).orElse(null);
                r.setContract(c);
                if(c != null){
                    r.setCustomer(cusr.findById(c.getCustomerId()).orElse(null));
                    r.setProjects(pr.findById(c.getProjectsId()).orElse(null));
                    r.setUser(our.findById(c.getLeadingId()).orElse(null));
                }
            }else{
                if(r.getCustomerId() != null){
                    r.setCustomer(cusr.findById(r.getCustomerId()).orElse(null));
                }
                if(r.getProductId() != null){
                    r.setProjects(pr.findById(r.getProjectsId()).orElse(null));
                }
                if(r.getUserId() != null){
                    r.setUser(our.findById(r.getUserId()).orElse(null));
                }
                if(r.getProductId() != null){
                    r.setProduct(pror.findById(r.getProductId()).orElse(null));
                }
            }
        }
        return rs;
    }

    public Receivables returnUtil(Receivables r){
//            if(r.getContractId() == null){
//                return r;
//            }
//            Contract c = cr.findById(r.getContractId()).orElse(null);
//            r.setContract(c);
            if(r.getContractId() != null && !"".equals(r.getContractId())){
                Contract c = cr.findById(r.getContractId()).orElse(null);
                r.setContract(c);
                if(c != null){
                    r.setCustomer(cusr.findById(c.getCustomerId()).orElse(null));
                    r.setProjects(pr.findById(c.getProjectsId()).orElse(null));
                    r.setUser(our.findById(c.getLeadingId()).orElse(null));
                }
            }else{
                if(r.getCustomerId() != null){
                    r.setCustomer(cusr.findById(r.getCustomerId()).orElse(null));
                }
                if(r.getProductId() != null){
                    r.setProjects(pr.findById(r.getProjectsId()).orElse(null));
                }
                if(r.getUserId() != null){
                    r.setUser(our.findById(r.getUserId()).orElse(null));
                }
                if(r.getProductId() != null){
                    r.setProduct(pror.findById(r.getProductId()).orElse(null));
                }
            }
        return r;
    }
}
